package com.threegroup.admin.controller;

import com.threegroup.admin.annotation.AuthorityVerify.AuthorityVerify;
import com.threegroup.admin.dto.SysDeptDTO;
import com.threegroup.admin.service.ISysDeptService;
import com.threegroup.common.utils.Result;
import com.threegroup.common.validator.AssertUtils;
import com.threegroup.common.validator.ValidatorUtils;
import com.threegroup.common.validator.group.AddGroup;
import com.threegroup.common.validator.group.DefaultGroup;
import com.threegroup.common.validator.group.UpdateGroup;
import kotlin.random.Random;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/sys/dept")
public class SysDeptController {

    @Resource
    private ISysDeptService sysDeptService;

    /**
     * 获取部门列表
     * @return
     */
    @AuthorityVerify("sys:dept:list")
    @GetMapping("/list")
    public Result<List<SysDeptDTO>> list() {
        List<SysDeptDTO> list = sysDeptService.list(new HashMap<>(1));

        return new Result<List<SysDeptDTO>>().ok(list);
    }

    /**
     * 新增部门
     * @return
     */
    @AuthorityVerify("sys:dept:save")
    @PostMapping
    public Result save(@RequestBody SysDeptDTO sysDeptDTO) {

        // 参数校验
        ValidatorUtils.validateEntity(sysDeptDTO, AddGroup.class, DefaultGroup.class);

        sysDeptService.save(sysDeptDTO);

        return new Result();
    }

    /**
     * 获取部门信息
     * @param id 部门id
     * @return
     */
    @AuthorityVerify("sys:dept:info")
    @GetMapping("{id}")
    public Result<SysDeptDTO> get(@PathVariable("id") Long id){
        SysDeptDTO data = sysDeptService.get(id);

        return new Result<SysDeptDTO>().ok(data);
    }

    /**
     * 更新部门信息
     * @param dto
     * @return
     */
    @AuthorityVerify("sys:dept:update")
    @PutMapping
    public Result update(@RequestBody SysDeptDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        sysDeptService.update(dto);

        return new Result();
    }

    /**
     * 删除部门信息
     * @param id
     * @return
     */
    @AuthorityVerify("sys:dept:delete")
    @DeleteMapping("{id}")
    public Result delete(@PathVariable("id") Long id){
        //效验数据
        AssertUtils.isNull(id, "id");

        sysDeptService.delete(id);

        return new Result();
    }
}
